package ${basepackage}.service.${module};

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import ${basepackage}.dao.${module}.${table.className}Dao;
import ${basepackage}.entity.${module}.${table.className};
import com.masget.business.enums.EnableState;
import com.masget.business.utils.ApplicationLogging;
import com.masget.business.utils.QueryWrapperUtils;
import com.masget.business.utils.QueryWrapperUtils.QueryWrapper;
import com.masget.command.utils.MaxIdUtil;
import com.masget.utils.MgException;
import com.masget.business.utils.ApplicationLogging;



/**
 * <b>function:</b> ${table.remarks!className}服务接口业务实现
 * @author ${author}
 * @createDate <#if now??>${now?string('yyyy-MM-dd HH:mm:ss')}</#if>
 * @file ${table.className}.java
 * @package ${basepackage}.service.${module}
 * @project ${app}
 * @blog ${blog}
 * @email ${email}
 * @version 1.0
 */
@Service
public class ${table.className}Service<T extends ${table.className}> extends ApplicationLogging {

	@Autowired
	private ${table.className}Dao<T> dao;
	
	public boolean add(T entity) throws Exception {
		logger.debug("添加[${table.remarks!className}]数据参数：{}", entity);
		
		if (entity != null) {
			
			Date now = new Date();
			if (entity.get${table.pkColumn.columnName}() == null) {
				long ${table.pkColumn.columnNameLower} = MaxIdUtil.getPrimaryKey("${table.schema}.${table.tableName}.${table.pkColumn.sqlName}");
				entity.set${table.pkColumn.columnName}(${table.pkColumn.columnNameLower});
			}
			<@addOperate />
		}
		
		boolean flag = false;
		try {
			flag = dao.add(entity) > 0;
        } catch (DuplicateKeyException e) {
        	throw new RuntimeException("输入信息已存在，违反数据唯一约束，请检查数据参数");
        } catch (Exception e) {
	        logger.error(e.getMessage(), e);
	        throw e;
        }
		
		logger.debug("添加[${table.remarks!className}]数据是否成功：{}", flag);
		return flag;
	}
	
	public boolean batch(List<T> entities) throws Exception {
		logger.debug("批量添加[${table.remarks!className}]数据参数：{}", entities.size());
		
		boolean flag = false;
		try {
			if (entities != null && !entities.isEmpty()) {
				
				int count = dao.batch(entities);
				logger.debug("批量入库${table.remarks!className}数据：{}", count);
				
				if (count != entities.size()) {
					throw new MgException(10001, "批量入库${table.remarks!className}数据不完整");
				} 
			}
			
			flag = true;
        } catch (DuplicateKeyException e) {
        	throw new RuntimeException("输入信息已存在，违反数据唯一约束，请检查数据参数");
        } catch (Exception e) {
	        logger.error(e.getMessage(), e);
	        throw e;
        }
		
		logger.debug("批量添加[${table.remarks!className}]数据是否成功：{}", flag);
		return flag;
	}
	
    public boolean edit(T entity) throws Exception {
		logger.debug("修改[${table.remarks!className}]数据参数：{}", entity);
		
		<@editOperate />
		
		boolean flag = false;
		try {
			flag = dao.edit(entity) > 0;
        } catch (DuplicateKeyException e) {
        	throw new RuntimeException("输入信息已存在，违反数据唯一约束，请检查数据参数");
        } catch (Exception e) {
	        logger.error(e.getMessage(), e);
	        throw e;
        }
		
		logger.debug("修改[${table.remarks!className}]数据是否成功：{}", flag);
		return flag;
	}
	
    public boolean remove(T entity) throws Exception {
		logger.debug("删除[${table.remarks!className}]数据参数：{}", entity);
		
		boolean flag = false;
		try {
			flag = dao.remove(entity) > 0;
        } catch (Exception e) {
	        logger.error(e.getMessage(), e);
	        throw e;
        }
		
		logger.debug("删除[${table.remarks!className}]数据是否成功：{}", flag);
		return flag;
	}
	
    public List<T> list(T entity) throws Exception {
		logger.debug("动态查询[${table.remarks!className}]数据参数 Entity：{}", entity);
		
		List<T> result = new ArrayList<T>();
		try {
			result = dao.query(entity, null);
        } catch (Exception e) {
	        logger.error(e.getMessage(), e);
	        throw e;
        }
		
		logger.debug("动态查询[${table.remarks!className}]数据结果集：{}", result);
		return result;
	}
	
    public T get(T entity) throws Exception {
		logger.debug("查询[${table.remarks!className}]数据参数 Entity：{}", entity);
		
		try {
			if (entity == null || entity.get${table.pkColumn.columnName}() == null) {
				return null;
			}
			
			entity = dao.get(entity);
        } catch (Exception e) {
	        logger.error(e.getMessage(), e);
	        throw e;
        }
		
		logger.debug("查询[${table.remarks!className}]数据结果集：{}", entity);
		return entity;
	}
	
    public T find(T entity) throws Exception {
		logger.debug("查询[${table.remarks!className}]数据参数 Entity：{}", entity);
		
		List<T> result = null;
		try {
			result = dao.query(entity, QueryWrapperUtils.builder(1, 1));
			if (result != null && !result.isEmpty()) {
				entity = result.get(0);
			} else {
				entity = null;
			}
        } catch (Exception e) {
	        logger.error(e.getMessage(), e);
	        throw e;
        }
		
		logger.debug("查询[${table.remarks!className}]数据结果集：{}", entity);
		return entity;
	}
	
    public List<T> query(T entity, QueryWrapper wrapper) throws Exception {
		logger.debug("动态查询[${table.remarks!className}]数据参数 Entity：{}， wrapper：{}", entity, wrapper);
		
		List<T> result = new ArrayList<T>();
		try {
			result = dao.query(entity, wrapper);
        } catch (Exception e) {
	        logger.error(e.getMessage(), e);
	        throw e;
        }
		
		logger.debug("动态查询[${table.remarks!className}]数据结果集：{}", result);
		return result;
	}
    
	public Long count(T entity) throws Exception {
		logger.debug("查询记录总条数[${table.remarks!className}]数据参数 Entity：{}", entity);
		
		Long count = 0L;
		try {
			count = dao.count(entity);
        } catch (Exception e) {
	        logger.error(e.getMessage(), e);
	        throw e;
        }
		
		logger.debug("查询记录总条数[${table.remarks!className}]数据结果：{}", count);
		return count;
	}
}
